package com.ocom.webapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.dto.Department;
import com.ocom.common.entity.company.EntityCompanyInfo;
import com.ocom.common.entity.human.EntityDeptInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.recode.AccessRecordVo;
import com.ocom.webapi.feign.AccessClientService;
import com.ocom.webapi.feign.CompanyClientService;
import com.ocom.webapi.feign.HumanClientService;
import com.ocom.webapi.service.QuarantinePersonService;
import com.ocom.webapi.vo.quarantineperson.OnShowVo;
import com.ocom.webapi.vo.quarantineperson.YkmSumVo;
import com.ocom.webapi.vo.quarantineperson.YmDataGroupByMachVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Slf4j
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class QuarantineCodeRecordController {
    private final Logger log = LoggerFactory.getLogger(QuarantineCodeRecordController.class);

    @Autowired
    private HumanClientService humanClientService;


    @Autowired
    private AccessClientService accessClientService;

    @Autowired
    private QuarantinePersonService quarantinePersonService;

    @Autowired
    private CompanyClientService companyClientService;
    /*
     * @Description:健康码统计列表（QuarantineCodeRecord/onShow）
     * @Author: lu 
     * @Date: 2022/4/21 15:50
     * @param comId:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(WebApiServiceUrl.URL_QuarantineCodeRecordOnShow)
    public Result MealPageOnShow(@RequestBody JSONObject jsonObject){
         if (jsonObject.getLong("comId")==null || jsonObject.getInteger("page")==null || jsonObject.getInteger("size")==null){
             return ResultUtil.error(ResultEnum.Err_203);
         }
        Department deleteDeptDto=new Department();
        deleteDeptDto.setComId(jsonObject.getLong("comId"));
        deleteDeptDto.setPage(jsonObject.getInteger("page"));
        deleteDeptDto.setSize(jsonObject.getInteger("size"));
        //当deptId为空或-1时，默认查询单位下所有部门，当前查询部门级别=0；统计汇总默认呈现下一级部门=1；
        JSONObject retuJSONObject=new JSONObject();
        if (jsonObject.getLong("deptId")!=null && jsonObject.getLong("deptId")!=-1){
            deleteDeptDto.setDeptId(jsonObject.getLong("deptId"));
            Result departmentGetDeptInfos=humanClientService.departmentGetDeptInfos(deleteDeptDto);
            if (departmentGetDeptInfos.getCode()==200){
                JSONObject retObject=JSON.parseObject(JSON.toJSONString(departmentGetDeptInfos.getData()));
                retuJSONObject.put("total",retObject.getLong("total")+1);
                List<EntityDeptInfo> entityDeptInfos= JSON.parseArray(JSON.toJSONString(retObject.get("list")), EntityDeptInfo.class);
                //若@deptList不为空，从第 (page-1)*size 个元素开始循环处理@deptList，处理size个元素后结束，返回total为@deptList元素数
                List<JSONObject> objectList=new ArrayList<>();

                JSONObject companyJSONObject=new JSONObject();
                if (jsonObject.getLong("deptId")==0){
                    Map<String, Object> map = new HashMap<>();
                    map.put("comId",jsonObject.getLong("comId"));
                    Result companyClientServiceGetDeptInfo=companyClientService.GetCompanyInfo(map);
                    if (companyClientServiceGetDeptInfo.getCode()==200){
                        EntityCompanyInfo entityCompanyInfo= JSON.parseObject(JSON.toJSONString(companyClientServiceGetDeptInfo.getData()), EntityCompanyInfo.class);
                        companyJSONObject.put("deptName",entityCompanyInfo.getComName());
                    }
                    companyJSONObject.put("deptId",0);
                    companyJSONObject.put("comId",jsonObject.getLong("comId"));
                }else {
                    Result bootparentGetDeptInfo=humanClientService.GetDeptInfo(jsonObject.getLong("comId"),jsonObject.getLong("deptId"),"");
                    if (bootparentGetDeptInfo.getCode()==200){
                        List<EntityDeptInfo> deptInfos= JSON.parseArray(JSON.toJSONString(bootparentGetDeptInfo.getData()), EntityDeptInfo.class);
                        for (EntityDeptInfo deptInfo : deptInfos) {
                            companyJSONObject.put("deptName",deptInfo.getDeptName());
                        }
                    }
                    companyJSONObject.put("deptId",jsonObject.getLong("deptId"));
                    companyJSONObject.put("comId",jsonObject.getLong("comId"));
                }

                JSONObject getYkmPersonnelSumJSONObjects=new JSONObject();

                getYkmPersonnelSumJSONObjects.put("comId",jsonObject.getLong("comId"));
                getYkmPersonnelSumJSONObjects.put("deptIds",jsonObject.getString("deptId"));
                Result getYkmPersonnelSums=accessClientService.getYkmPersonnelSum(getYkmPersonnelSumJSONObjects);
                if (getYkmPersonnelSums.getCode()==200){
                    JSONObject reYkmPersonnelSum=JSON.parseObject(JSON.toJSONString(getYkmPersonnelSums.getData()));
                    Integer personNums=reYkmPersonnelSum.getInteger("green")+reYkmPersonnelSum.getInteger("yellow")+reYkmPersonnelSum.getInteger("red")+reYkmPersonnelSum.getInteger("other")+reYkmPersonnelSum.getInteger("pullFailed");
                    Integer hsYinNums=reYkmPersonnelSum.getInteger("hours24")+reYkmPersonnelSum.getInteger("hours48")+reYkmPersonnelSum.getInteger("hours72")+reYkmPersonnelSum.getInteger("within7Days")+reYkmPersonnelSum.getInteger("more7Days");
                    companyJSONObject.put("personNums",personNums);
                    companyJSONObject.put("greenNums",reYkmPersonnelSum.getInteger("green"));
                    companyJSONObject.put("yellowNums",reYkmPersonnelSum.getInteger("yellow"));
                    companyJSONObject.put("redNums",reYkmPersonnelSum.getInteger("red"));
                    companyJSONObject.put("otherNums",reYkmPersonnelSum.getInteger("other"));
                    companyJSONObject.put("pullFailedNums",reYkmPersonnelSum.getInteger("pullFailed"));
                    companyJSONObject.put("hsYinNums",hsYinNums);
                    companyJSONObject.put("hsYangNums",reYkmPersonnelSum.getInteger("positive"));
                }else {
                    companyJSONObject.put("personNums",0);
                    companyJSONObject.put("greenNums",0);
                    companyJSONObject.put("yellowNums",0);
                    companyJSONObject.put("redNums",0);
                    companyJSONObject.put("otherNums",0);
                    companyJSONObject.put("pullFailedNums",0);
                    companyJSONObject.put("hsYinNums",0);
                    companyJSONObject.put("hsYangNums",0);
                }
                objectList.add(companyJSONObject);
                if (entityDeptInfos.size()>0){
                    for (EntityDeptInfo entityDeptInfo : entityDeptInfos) {
                        JSONObject rebJSONObject=JSON.parseObject(JSON.toJSONString(entityDeptInfo));
                        Department department=new Department();
                        department.setComId(jsonObject.getLong("comId"));
                        department.setDeptId(entityDeptInfo.getDeptId());
                        department.setShowLevel(0);
                        Result bootparentDeptId=humanClientService.getDeptIds(department);
                        if (bootparentDeptId.getCode()==200){
                            JSONObject bootparentDeptIdObject=JSON.parseObject(JSON.toJSONString(bootparentDeptId.getData()));
                            //(2)若@deptIds为空，则默认该部门统计数皆为0；
                            //(3)若@deptIds不为空，获取该部门统计汇总，调用3.0微服务-门禁getYkmPersonnelSum(comId,@deptIds);
                            JSONObject getYkmPersonnelSumJSONObject=new JSONObject();
                            String deptIds="";
                            if (bootparentDeptIdObject.getString("deptIds").equals("")){
                                deptIds= String.valueOf(entityDeptInfo.getDeptId());
                            }else {
                                deptIds=entityDeptInfo.getDeptId()+","+bootparentDeptIdObject.getString("deptIds");
                            }

                            getYkmPersonnelSumJSONObject.put("comId",jsonObject.getLong("comId"));
                            getYkmPersonnelSumJSONObject.put("deptIds",deptIds);
                            Result getYkmPersonnelSum=accessClientService.getYkmPersonnelSum(getYkmPersonnelSumJSONObject);
                            if (getYkmPersonnelSum.getCode()==200){
                                JSONObject reYkmPersonnelSum=JSON.parseObject(JSON.toJSONString(getYkmPersonnelSum.getData()));
                                Integer personNums=reYkmPersonnelSum.getInteger("green")+reYkmPersonnelSum.getInteger("yellow")+reYkmPersonnelSum.getInteger("red")+reYkmPersonnelSum.getInteger("other")+reYkmPersonnelSum.getInteger("pullFailed");
                                Integer hsYinNums=reYkmPersonnelSum.getInteger("hours24")+reYkmPersonnelSum.getInteger("hours48")+reYkmPersonnelSum.getInteger("hours72")+reYkmPersonnelSum.getInteger("within7Days")+reYkmPersonnelSum.getInteger("more7Days");
                                rebJSONObject.put("personNums",personNums);
                                rebJSONObject.put("greenNums",reYkmPersonnelSum.getInteger("green"));
                                rebJSONObject.put("yellowNums",reYkmPersonnelSum.getInteger("yellow"));
                                rebJSONObject.put("redNums",reYkmPersonnelSum.getInteger("red"));
                                rebJSONObject.put("otherNums",reYkmPersonnelSum.getInteger("other"));
                                rebJSONObject.put("pullFailedNums",reYkmPersonnelSum.getInteger("pullFailed"));
                                rebJSONObject.put("hsYinNums",hsYinNums);
                                rebJSONObject.put("hsYangNums",reYkmPersonnelSum.getInteger("positive"));
                            }else {
                                rebJSONObject.put("personNums",0);
                                rebJSONObject.put("greenNums",0);
                                rebJSONObject.put("yellowNums",0);
                                rebJSONObject.put("redNums",0);
                                rebJSONObject.put("otherNums",0);
                                rebJSONObject.put("pullFailedNums",0);
                                rebJSONObject.put("hsYinNums",0);
                                rebJSONObject.put("hsYangNums",0);
                            }
                            objectList.add(rebJSONObject);
                        }else {
                            return bootparentDeptId;
                        }
                    }
                    retuJSONObject.put("list",objectList);
                    return ResultUtil.success(retuJSONObject);
                }else {
                    retuJSONObject.put("list",objectList);
                    retuJSONObject.put("total",1);
                    return ResultUtil.success(retuJSONObject);
                }
            }else {
                return departmentGetDeptInfos;
            }
        }else {
            List<JSONObject> objectList=new ArrayList<>();
            JSONObject rebJSONObject=new JSONObject();
            Department department=new Department();
            department.setComId(jsonObject.getLong("comId"));
            department.setDeptId(-1L);
            department.setShowLevel(0);
            retuJSONObject.put("total",1);
            Result bootparentDeptId=humanClientService.getDeptIds(department);
            JSONObject bootparentDeptIdObject=JSON.parseObject(JSON.toJSONString(bootparentDeptId.getData()));
            //(2)若@deptIds为空，则默认该部门统计数皆为0；
            //(3)若@deptIds不为空，获取该部门统计汇总，调用3.0微服务-门禁getYkmPersonnelSum(comId,@deptIds);
            JSONObject getYkmPersonnelSumJSONObject=new JSONObject();
            String deptIds="";
            if (StringUtils.isEmpty(bootparentDeptIdObject.getString("deptIds"))){
                deptIds="0";
            }else {
                deptIds="0,"+bootparentDeptIdObject.getString("deptIds");
            }

            getYkmPersonnelSumJSONObject.put("comId",jsonObject.getLong("comId"));
            getYkmPersonnelSumJSONObject.put("deptIds",deptIds);
            Result getYkmPersonnelSum=accessClientService.getYkmPersonnelSum(getYkmPersonnelSumJSONObject);
            Map<String, Object> map = new HashMap<>();
            map.put("comId",jsonObject.getLong("comId"));
            Result companyClientServiceGetDeptInfo=companyClientService.GetCompanyInfo(map);
            if (companyClientServiceGetDeptInfo.getCode()==200){
                EntityCompanyInfo entityCompanyInfo= JSON.parseObject(JSON.toJSONString(companyClientServiceGetDeptInfo.getData()), EntityCompanyInfo.class);
                rebJSONObject.put("deptName",entityCompanyInfo.getComName());
            }
            rebJSONObject.put("deptId",0);
            rebJSONObject.put("comId",jsonObject.getLong("comId"));
            if (getYkmPersonnelSum.getCode()==200){
                JSONObject reYkmPersonnelSum=JSON.parseObject(JSON.toJSONString(getYkmPersonnelSum.getData()));
                Integer personNums=reYkmPersonnelSum.getInteger("green")+reYkmPersonnelSum.getInteger("yellow")+reYkmPersonnelSum.getInteger("red")+reYkmPersonnelSum.getInteger("other")+reYkmPersonnelSum.getInteger("pullFailed");
                Integer hsYinNums=reYkmPersonnelSum.getInteger("hours24")+reYkmPersonnelSum.getInteger("hours48")+reYkmPersonnelSum.getInteger("hours72")+reYkmPersonnelSum.getInteger("within7Days")+reYkmPersonnelSum.getInteger("more7Days");
                rebJSONObject.put("personNums",personNums);
                rebJSONObject.put("greenNums",reYkmPersonnelSum.getInteger("green"));
                rebJSONObject.put("yellowNums",reYkmPersonnelSum.getInteger("yellow"));
                rebJSONObject.put("redNums",reYkmPersonnelSum.getInteger("red"));
                rebJSONObject.put("otherNums",reYkmPersonnelSum.getInteger("other"));
                rebJSONObject.put("pullFailedNums",reYkmPersonnelSum.getInteger("pullFailed"));
                rebJSONObject.put("hsYinNums",hsYinNums);
                rebJSONObject.put("hsYangNums",reYkmPersonnelSum.getInteger("positive"));
            }else {
                rebJSONObject.put("personNums",0);
                rebJSONObject.put("greenNums",0);
                rebJSONObject.put("yellowNums",0);
                rebJSONObject.put("redNums",0);
                rebJSONObject.put("otherNums",0);
                rebJSONObject.put("pullFailedNums",0);
                rebJSONObject.put("hsYinNums",0);
                rebJSONObject.put("hsYangNums",0);
            }
            objectList.add(rebJSONObject);
            retuJSONObject.put("list",objectList);
            return ResultUtil.success(retuJSONObject);
        }

    }


    /*
     * @Description:核酸统计列表（QuarantineNucleicAcid/onShow）
     * @Author: lu 
     * @Date: 2022/4/22 15:34
     * @param jsonObject:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(WebApiServiceUrl.URL_QuarantineNucleicAcidOnShow)
    public Result quarantineNucleicAcidOnShow(@RequestBody JSONObject jsonObject){
        if (jsonObject.getLong("comId")==null || jsonObject.getInteger("page")==null || jsonObject.getInteger("size")==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        Department deleteDeptDto=new Department();
        deleteDeptDto.setComId(jsonObject.getLong("comId"));
        deleteDeptDto.setPage(jsonObject.getInteger("page"));
        deleteDeptDto.setSize(jsonObject.getInteger("size"));
        //当deptId为空或-1时，默认查询单位下所有部门，当前查询部门级别=0；统计汇总默认呈现下一级部门=1；
        JSONObject retuJSONObject=new JSONObject();
        if (jsonObject.getLong("deptId")!=null && jsonObject.getLong("deptId")!=-1){
            deleteDeptDto.setDeptId(jsonObject.getLong("deptId"));
            Result departmentGetDeptInfos=humanClientService.departmentGetDeptInfos(deleteDeptDto);
            if (departmentGetDeptInfos.getCode()==200){
                JSONObject retObject=JSON.parseObject(JSON.toJSONString(departmentGetDeptInfos.getData()));
                retuJSONObject.put("total",retObject.getLong("total")+1);
                List<EntityDeptInfo> entityDeptInfos= JSON.parseArray(JSON.toJSONString(retObject.get("list")), EntityDeptInfo.class);
                //若@deptList不为空，从第 (page-1)*size 个元素开始循环处理@deptList，处理size个元素后结束，返回total为@deptList元素数

                List<JSONObject> objectList=new ArrayList<>();

                JSONObject companyJSONObject=new JSONObject();
                if (jsonObject.getLong("deptId")==0){
                    Map<String, Object> map = new HashMap<>();
                    map.put("comId",jsonObject.getLong("comId"));
                    Result companyClientServiceGetDeptInfo=companyClientService.GetCompanyInfo(map);
                    if (companyClientServiceGetDeptInfo.getCode()==200){
                        EntityCompanyInfo entityCompanyInfo= JSON.parseObject(JSON.toJSONString(companyClientServiceGetDeptInfo.getData()), EntityCompanyInfo.class);
                        companyJSONObject.put("deptName",entityCompanyInfo.getComName());
                    }
                    companyJSONObject.put("deptId",0);
                    companyJSONObject.put("comId",jsonObject.getLong("comId"));
                }else {
                    Result bootparentGetDeptInfo=humanClientService.GetDeptInfo(jsonObject.getLong("comId"),jsonObject.getLong("deptId"),"");
                    if (bootparentGetDeptInfo.getCode()==200){
                        List<EntityDeptInfo> deptInfos= JSON.parseArray(JSON.toJSONString(bootparentGetDeptInfo.getData()), EntityDeptInfo.class);
                        for (EntityDeptInfo deptInfo : deptInfos) {
                            companyJSONObject.put("deptName",deptInfo.getDeptName());
                        }
                    }
                    companyJSONObject.put("deptId",jsonObject.getLong("deptId"));
                    companyJSONObject.put("comId",jsonObject.getLong("comId"));
                }

                JSONObject getYkmPersonnelSumJSONObjects=new JSONObject();

                getYkmPersonnelSumJSONObjects.put("comId",jsonObject.getLong("comId"));
                getYkmPersonnelSumJSONObjects.put("deptIds",jsonObject.getString("deptId"));
                Result getYkmPersonnelSums=accessClientService.getYkmPersonnelSum(getYkmPersonnelSumJSONObjects);
                if (getYkmPersonnelSums.getCode()==200){
                    JSONObject reYkmPersonnelSum=JSON.parseObject(JSON.toJSONString(getYkmPersonnelSums.getData()));
                    Integer personNums=reYkmPersonnelSum.getInteger("green")+reYkmPersonnelSum.getInteger("yellow")+reYkmPersonnelSum.getInteger("red")+reYkmPersonnelSum.getInteger("other")+reYkmPersonnelSum.getInteger("pullFailed");
                    Integer hsYinNums=reYkmPersonnelSum.getInteger("hours24")+reYkmPersonnelSum.getInteger("hours48")+reYkmPersonnelSum.getInteger("hours72")+reYkmPersonnelSum.getInteger("within7Days")+reYkmPersonnelSum.getInteger("more7Days");
                    companyJSONObject.put("personNums",personNums);
                    companyJSONObject.put("hours24",reYkmPersonnelSum.getInteger("hours24"));
                    companyJSONObject.put("hours48",reYkmPersonnelSum.getInteger("hours48"));
                    companyJSONObject.put("hours72",reYkmPersonnelSum.getInteger("hours72"));
                    companyJSONObject.put("more7Days",reYkmPersonnelSum.getInteger("more7Days"));
                    companyJSONObject.put("within7Days",reYkmPersonnelSum.getInteger("within7Days"));
                    companyJSONObject.put("noNucleic",reYkmPersonnelSum.getInteger("noNucleic"));
                    companyJSONObject.put("pullFailed",reYkmPersonnelSum.getInteger("pullFailed"));
                    companyJSONObject.put("hsYinNums",hsYinNums);
                    companyJSONObject.put("hsYangNums",reYkmPersonnelSum.getInteger("positive"));
                }else {
                    companyJSONObject.put("personNums",0);
                    companyJSONObject.put("hours24",0);
                    companyJSONObject.put("hours48",0);
                    companyJSONObject.put("hours72",0);
                    companyJSONObject.put("more7Days",0);
                    companyJSONObject.put("within7Days",0);
                    companyJSONObject.put("noNucleic",0);
                    companyJSONObject.put("pullFailed",0);
                    companyJSONObject.put("hsYinNums",0);
                    companyJSONObject.put("hsYangNums",0);
                }
                objectList.add(companyJSONObject);
                if (entityDeptInfos.size()>0){
                    for (EntityDeptInfo entityDeptInfo : entityDeptInfos) {
                        JSONObject rebJSONObject=JSON.parseObject(JSON.toJSONString(entityDeptInfo));
                        Department department=new Department();
                        department.setComId(jsonObject.getLong("comId"));
                        department.setDeptId(entityDeptInfo.getDeptId());
                        department.setShowLevel(0);
                        Result bootparentDeptId=humanClientService.getDeptIds(department);
                        if (bootparentDeptId.getCode()==200){
                            JSONObject bootparentDeptIdObject=JSON.parseObject(JSON.toJSONString(bootparentDeptId.getData()));
                            //(2)若@deptIds为空，则默认该部门统计数皆为0；
                            //(3)若@deptIds不为空，获取该部门统计汇总，调用3.0微服务-门禁getYkmPersonnelSum(comId,@deptIds);
                            JSONObject getYkmPersonnelSumJSONObject=new JSONObject();
                            String deptIds="";
                            if (bootparentDeptIdObject.getString("deptIds").equals("")){
                                deptIds= String.valueOf(entityDeptInfo.getDeptId());
                            }else {
                                deptIds=entityDeptInfo.getDeptId()+","+bootparentDeptIdObject.getString("deptIds");
                            }

                            getYkmPersonnelSumJSONObject.put("comId",jsonObject.getLong("comId"));
                            getYkmPersonnelSumJSONObject.put("deptIds",deptIds);
                            Result getYkmPersonnelSum=accessClientService.getYkmPersonnelSum(getYkmPersonnelSumJSONObject);
                            if (getYkmPersonnelSum.getCode()==200){
                                JSONObject reYkmPersonnelSum=JSON.parseObject(JSON.toJSONString(getYkmPersonnelSum.getData()));
                                Integer personNums=reYkmPersonnelSum.getInteger("green")+reYkmPersonnelSum.getInteger("yellow")+reYkmPersonnelSum.getInteger("red")+reYkmPersonnelSum.getInteger("other")+reYkmPersonnelSum.getInteger("pullFailed");
                                Integer hsYinNums=reYkmPersonnelSum.getInteger("hours24")+reYkmPersonnelSum.getInteger("hours48")+reYkmPersonnelSum.getInteger("hours72")+reYkmPersonnelSum.getInteger("within7Days")+reYkmPersonnelSum.getInteger("more7Days");
                                rebJSONObject.put("personNums",personNums);
                                rebJSONObject.put("hours24",reYkmPersonnelSum.getInteger("hours24"));
                                rebJSONObject.put("hours48",reYkmPersonnelSum.getInteger("hours48"));
                                rebJSONObject.put("hours72",reYkmPersonnelSum.getInteger("hours72"));
                                rebJSONObject.put("more7Days",reYkmPersonnelSum.getInteger("more7Days"));
                                rebJSONObject.put("within7Days",reYkmPersonnelSum.getInteger("within7Days"));
                                rebJSONObject.put("noNucleic",reYkmPersonnelSum.getInteger("noNucleic"));
                                rebJSONObject.put("pullFailed",reYkmPersonnelSum.getInteger("pullFailed"));
                                rebJSONObject.put("hsYinNums",hsYinNums);
                                rebJSONObject.put("hsYangNums",reYkmPersonnelSum.getInteger("positive"));
                            }else {
                                rebJSONObject.put("personNums",0);
                                rebJSONObject.put("hours24",0);
                                rebJSONObject.put("hours48",0);
                                rebJSONObject.put("hours72",0);
                                rebJSONObject.put("more7Days",0);
                                rebJSONObject.put("within7Days",0);
                                rebJSONObject.put("noNucleic",0);
                                rebJSONObject.put("pullFailed",0);
                                rebJSONObject.put("hsYinNums",0);
                                rebJSONObject.put("hsYangNums",0);
                            }
                            objectList.add(rebJSONObject);
                        }else {
                            return bootparentDeptId;
                        }
                    }
                    retuJSONObject.put("list",objectList);
                    return ResultUtil.success(retuJSONObject);
                }else {
                    retuJSONObject.put("list",objectList);
                    retuJSONObject.put("total",1);
                    return ResultUtil.success(retuJSONObject);
                }
            }else {
                return departmentGetDeptInfos;
            }
        }else {
            List<JSONObject> objectList=new ArrayList<>();
            JSONObject rebJSONObject=new JSONObject();
            Department department=new Department();
            department.setComId(jsonObject.getLong("comId"));
            department.setDeptId(-1L);
            department.setShowLevel(0);
            retuJSONObject.put("total",1);
            Result bootparentDeptId=humanClientService.getDeptIds(department);

            Map<String, Object> map = new HashMap<>();
            map.put("comId",jsonObject.getLong("comId"));
            Result companyClientServiceGetDeptInfo=companyClientService.GetCompanyInfo(map);
            if (companyClientServiceGetDeptInfo.getCode()==200){
                EntityCompanyInfo entityCompanyInfo= JSON.parseObject(JSON.toJSONString(companyClientServiceGetDeptInfo.getData()), EntityCompanyInfo.class);
                rebJSONObject.put("deptName",entityCompanyInfo.getComName());
            }
            rebJSONObject.put("deptId",0);
            rebJSONObject.put("comId",jsonObject.getLong("comId"));

            JSONObject bootparentDeptIdObject=JSON.parseObject(JSON.toJSONString(bootparentDeptId.getData()));
            //(2)若@deptIds为空，则默认该部门统计数皆为0；
            //(3)若@deptIds不为空，获取该部门统计汇总，调用3.0微服务-门禁getYkmPersonnelSum(comId,@deptIds);
            JSONObject getYkmPersonnelSumJSONObject=new JSONObject();
            String deptIds="";
            if (StringUtils.isEmpty(bootparentDeptIdObject.getString("deptIds"))){
                deptIds="0";
            }else {
                deptIds="0,"+bootparentDeptIdObject.getString("deptIds");
            }


            getYkmPersonnelSumJSONObject.put("comId",jsonObject.getLong("comId"));
            getYkmPersonnelSumJSONObject.put("deptIds",deptIds);
            Result getYkmPersonnelSum=accessClientService.getYkmPersonnelSum(getYkmPersonnelSumJSONObject);
            if (getYkmPersonnelSum.getCode()==200){
                JSONObject reYkmPersonnelSum=JSON.parseObject(JSON.toJSONString(getYkmPersonnelSum.getData()));
                Integer personNums=reYkmPersonnelSum.getInteger("green")+reYkmPersonnelSum.getInteger("yellow")+reYkmPersonnelSum.getInteger("red")+reYkmPersonnelSum.getInteger("other")+reYkmPersonnelSum.getInteger("pullFailed");
                Integer hsYinNums=reYkmPersonnelSum.getInteger("hours24")+reYkmPersonnelSum.getInteger("hours48")+reYkmPersonnelSum.getInteger("hours72")+reYkmPersonnelSum.getInteger("within7Days")+reYkmPersonnelSum.getInteger("more7Days");
                rebJSONObject.put("personNums",personNums);
                rebJSONObject.put("hours24",reYkmPersonnelSum.getInteger("hours24"));
                rebJSONObject.put("hours48",reYkmPersonnelSum.getInteger("hours48"));
                rebJSONObject.put("hours72",reYkmPersonnelSum.getInteger("hours72"));
                rebJSONObject.put("more7Days",reYkmPersonnelSum.getInteger("more7Days"));
                rebJSONObject.put("within7Days",reYkmPersonnelSum.getInteger("within7Days"));
                rebJSONObject.put("noNucleic",reYkmPersonnelSum.getInteger("noNucleic"));
                rebJSONObject.put("pullFailed",reYkmPersonnelSum.getInteger("pullFailed"));
                rebJSONObject.put("hsYinNums",hsYinNums);
                rebJSONObject.put("hsYangNums",reYkmPersonnelSum.getInteger("positive"));
            }else {
                rebJSONObject.put("personNums",0);
                rebJSONObject.put("hours24",0);
                rebJSONObject.put("hours48",0);
                rebJSONObject.put("hours72",0);
                rebJSONObject.put("more7Days",0);
                rebJSONObject.put("within7Days",0);
                rebJSONObject.put("noNucleic",0);
                rebJSONObject.put("pullFailed",0);
                rebJSONObject.put("hsYinNums",0);
                rebJSONObject.put("hsYangNums",0);
            }
            objectList.add(rebJSONObject);
            retuJSONObject.put("list",objectList);
            return ResultUtil.success(retuJSONObject);

        }

    }


    /*
     * @Description:7.6.1疫苗接种统计列表（QuarantineYmRecord/onShow）
     * @Author: lu 
     * @Date: 2022/4/22 16:43
     * @param jsonObject:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(WebApiServiceUrl.URL_QuarantineYmRecordOnShow)
    public Result quarantineYmRecordOnShow(@RequestBody JSONObject jsonObject){
        if (jsonObject.getLong("comId")==null || jsonObject.getInteger("page")==null || jsonObject.getInteger("size")==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        Department deleteDeptDto=new Department();
        deleteDeptDto.setComId(jsonObject.getLong("comId"));
        deleteDeptDto.setPage(jsonObject.getInteger("page"));
        deleteDeptDto.setSize(jsonObject.getInteger("size"));
        //当deptId为空或-1时，默认查询单位下所有部门，当前查询部门级别=0；统计汇总默认呈现下一级部门=1；
        JSONObject retuJSONObject=new JSONObject();
        if (jsonObject.getLong("deptId")!=null && jsonObject.getLong("deptId")!=-1){
            deleteDeptDto.setDeptId(jsonObject.getLong("deptId"));
            Result departmentGetDeptInfos=humanClientService.departmentGetDeptInfos(deleteDeptDto);
            if (departmentGetDeptInfos.getCode()==200){
                JSONObject retObject=JSON.parseObject(JSON.toJSONString(departmentGetDeptInfos.getData()));
                retuJSONObject.put("total",retObject.getLong("total")+1);
                List<EntityDeptInfo> entityDeptInfos= JSON.parseArray(JSON.toJSONString(retObject.get("list")), EntityDeptInfo.class);
                //若@deptList不为空，从第 (page-1)*size 个元素开始循环处理@deptList，处理size个元素后结束，返回total为@deptList元素数

                List<JSONObject> objectList=new ArrayList<>();

                JSONObject companyJSONObject=new JSONObject();
                if (jsonObject.getLong("deptId")==0){
                    Map<String, Object> map = new HashMap<>();
                    map.put("comId",jsonObject.getLong("comId"));
                    Result companyClientServiceGetDeptInfo=companyClientService.GetCompanyInfo(map);
                    if (companyClientServiceGetDeptInfo.getCode()==200){
                        EntityCompanyInfo entityCompanyInfo= JSON.parseObject(JSON.toJSONString(companyClientServiceGetDeptInfo.getData()), EntityCompanyInfo.class);
                        companyJSONObject.put("deptName",entityCompanyInfo.getComName());
                    }
                    companyJSONObject.put("deptId",0);
                    companyJSONObject.put("comId",jsonObject.getLong("comId"));
                }else {
                    Result bootparentGetDeptInfo=humanClientService.GetDeptInfo(jsonObject.getLong("comId"),jsonObject.getLong("deptId"),"");
                    if (bootparentGetDeptInfo.getCode()==200){
                        List<EntityDeptInfo> deptInfos= JSON.parseArray(JSON.toJSONString(bootparentGetDeptInfo.getData()), EntityDeptInfo.class);
                        for (EntityDeptInfo deptInfo : deptInfos) {
                            companyJSONObject.put("deptName",deptInfo.getDeptName());
                        }
                    }
                    companyJSONObject.put("deptId",jsonObject.getLong("deptId"));
                    companyJSONObject.put("comId",jsonObject.getLong("comId"));
                }

                JSONObject getYkmPersonnelSumJSONObjects=new JSONObject();

                getYkmPersonnelSumJSONObjects.put("comId",jsonObject.getLong("comId"));
                getYkmPersonnelSumJSONObjects.put("deptIds",jsonObject.getString("deptId"));
                Result getYkmPersonnelSums=accessClientService.getYkmPersonnelSum(getYkmPersonnelSumJSONObjects);
                if (getYkmPersonnelSums.getCode()==200){
                    JSONObject reYkmPersonnelSum=JSON.parseObject(JSON.toJSONString(getYkmPersonnelSums.getData()));
                    Integer personNums=reYkmPersonnelSum.getInteger("green")+reYkmPersonnelSum.getInteger("yellow")+reYkmPersonnelSum.getInteger("red")+reYkmPersonnelSum.getInteger("other")+reYkmPersonnelSum.getInteger("pullFailed");
                    companyJSONObject.put("personNums",personNums);
                    companyJSONObject.put("ym0",reYkmPersonnelSum.getLong("ym0"));
                    companyJSONObject.put("ym1",reYkmPersonnelSum.getLong("ym1"));
                    companyJSONObject.put("ym2",reYkmPersonnelSum.getLong("ym2"));
                    companyJSONObject.put("ym3",reYkmPersonnelSum.getLong("ym3"));
                    companyJSONObject.put("ymMore3",reYkmPersonnelSum.getLong("ymMore3"));
                    companyJSONObject.put("pullFailed",reYkmPersonnelSum.getLong("pullFailed"));
                }else {
                    companyJSONObject.put("personNums",0);
                    companyJSONObject.put("ym0",0);
                    companyJSONObject.put("ym1",0);
                    companyJSONObject.put("ym2",0);
                    companyJSONObject.put("ym3",0);
                    companyJSONObject.put("ymMore3",0);
                    companyJSONObject.put("pullFailed",0);
                }
                objectList.add(companyJSONObject);
                if (entityDeptInfos.size()>0){


                    for (EntityDeptInfo entityDeptInfo : entityDeptInfos) {
                        JSONObject rebJSONObject=JSON.parseObject(JSON.toJSONString(entityDeptInfo));
                        Department department=new Department();
                        department.setComId(jsonObject.getLong("comId"));
                        department.setDeptId(entityDeptInfo.getDeptId());
                        department.setShowLevel(0);
                        Result bootparentDeptId=humanClientService.getDeptIds(department);
                        if (bootparentDeptId.getCode()==200){
                            JSONObject bootparentDeptIdObject=JSON.parseObject(JSON.toJSONString(bootparentDeptId.getData()));
                            //(2)若@deptIds为空，则默认该部门统计数皆为0；
                            //(3)若@deptIds不为空，获取该部门统计汇总，调用3.0微服务-门禁getYkmPersonnelSum(comId,@deptIds);
                            JSONObject getYkmPersonnelSumJSONObject=new JSONObject();
                            String deptIds="";
                            if (bootparentDeptIdObject.getString("deptIds").equals("")){
                                deptIds= String.valueOf(entityDeptInfo.getDeptId());
                            }else {
                                deptIds=entityDeptInfo.getDeptId()+","+bootparentDeptIdObject.getString("deptIds");
                            }

                            getYkmPersonnelSumJSONObject.put("comId",jsonObject.getLong("comId"));
                            getYkmPersonnelSumJSONObject.put("deptIds",deptIds);
                            Result getYkmPersonnelSum=accessClientService.getYkmPersonnelSum(getYkmPersonnelSumJSONObject);
                            if (getYkmPersonnelSum.getCode()==200){
                                JSONObject reYkmPersonnelSum=JSON.parseObject(JSON.toJSONString(getYkmPersonnelSum.getData()));
                                Integer personNums=reYkmPersonnelSum.getInteger("green")+reYkmPersonnelSum.getInteger("yellow")+reYkmPersonnelSum.getInteger("red")+reYkmPersonnelSum.getInteger("other")+reYkmPersonnelSum.getInteger("pullFailed");
                                rebJSONObject.put("personNums",personNums);
                                rebJSONObject.put("ym0",reYkmPersonnelSum.getLong("ym0"));
                                rebJSONObject.put("ym1",reYkmPersonnelSum.getLong("ym1"));
                                rebJSONObject.put("ym2",reYkmPersonnelSum.getLong("ym2"));
                                rebJSONObject.put("ym3",reYkmPersonnelSum.getLong("ym3"));
                                rebJSONObject.put("ymMore3",reYkmPersonnelSum.getLong("ymMore3"));
                                rebJSONObject.put("pullFailed",reYkmPersonnelSum.getLong("pullFailed"));
                            }else {
                                rebJSONObject.put("personNums",0);
                                rebJSONObject.put("ym0",0);
                                rebJSONObject.put("ym1",0);
                                rebJSONObject.put("ym2",0);
                                rebJSONObject.put("ym3",0);
                                rebJSONObject.put("ymMore3",0);
                                rebJSONObject.put("pullFailed",0);
                            }
                            objectList.add(rebJSONObject);
                        }else {
                            return bootparentDeptId;
                        }
                    }
                    retuJSONObject.put("list",objectList);
                    return ResultUtil.success(retuJSONObject);
                }else {
                    retuJSONObject.put("list",objectList);
                    retuJSONObject.put("total",1);
                    return ResultUtil.success(retuJSONObject);
                }
            }else {
                return departmentGetDeptInfos;
            }
        }else {
            List<JSONObject> objectList=new ArrayList<>();
            JSONObject rebJSONObject=new JSONObject();
            Department department=new Department();
            department.setComId(jsonObject.getLong("comId"));
            department.setDeptId(-1L);
            department.setShowLevel(0);
            retuJSONObject.put("total",1);
            Result bootparentDeptId=humanClientService.getDeptIds(department);

            JSONObject bootparentDeptIdObject=JSON.parseObject(JSON.toJSONString(bootparentDeptId.getData()));
            //(2)若@deptIds为空，则默认该部门统计数皆为0；
            //(3)若@deptIds不为空，获取该部门统计汇总，调用3.0微服务-门禁getYkmPersonnelSum(comId,@deptIds);
            JSONObject getYkmPersonnelSumJSONObject=new JSONObject();
            Map<String, Object> map = new HashMap<>();
            map.put("comId",jsonObject.getLong("comId"));
            Result companyClientServiceGetDeptInfo=companyClientService.GetCompanyInfo(map);
            if (companyClientServiceGetDeptInfo.getCode()==200){
                EntityCompanyInfo entityCompanyInfo= JSON.parseObject(JSON.toJSONString(companyClientServiceGetDeptInfo.getData()), EntityCompanyInfo.class);
                rebJSONObject.put("deptName",entityCompanyInfo.getComName());
            }
            rebJSONObject.put("deptId",0);
            rebJSONObject.put("comId",jsonObject.getLong("comId"));
            String deptIds="";
            if (StringUtils.isEmpty(bootparentDeptIdObject.getString("deptIds"))){
                deptIds="0";
            }else {
                deptIds="0,"+bootparentDeptIdObject.getString("deptIds");
            }

            getYkmPersonnelSumJSONObject.put("comId",jsonObject.getLong("comId"));
            getYkmPersonnelSumJSONObject.put("deptIds",deptIds);
            Result getYkmPersonnelSum=accessClientService.getYkmPersonnelSum(getYkmPersonnelSumJSONObject);
            if (getYkmPersonnelSum.getCode()==200){
                JSONObject reYkmPersonnelSum=JSON.parseObject(JSON.toJSONString(getYkmPersonnelSum.getData()));
                Integer personNums=reYkmPersonnelSum.getInteger("green")+reYkmPersonnelSum.getInteger("yellow")+reYkmPersonnelSum.getInteger("red")+reYkmPersonnelSum.getInteger("other")+reYkmPersonnelSum.getInteger("pullFailed");
                rebJSONObject.put("personNums",personNums);
                rebJSONObject.put("ym0",reYkmPersonnelSum.getLong("ym0"));
                rebJSONObject.put("ym1",reYkmPersonnelSum.getLong("ym1"));
                rebJSONObject.put("ym2",reYkmPersonnelSum.getLong("ym2"));
                rebJSONObject.put("ym3",reYkmPersonnelSum.getLong("ym3"));
                rebJSONObject.put("ymMore3",reYkmPersonnelSum.getLong("ymMore3"));
                rebJSONObject.put("pullFailed",reYkmPersonnelSum.getLong("pullFailed"));
            }else {
                rebJSONObject.put("personNums",0);
                rebJSONObject.put("ym0",0);
                rebJSONObject.put("ym1",0);
                rebJSONObject.put("ym2",0);
                rebJSONObject.put("ym3",0);
                rebJSONObject.put("ymMore3",0);
                rebJSONObject.put("pullFailed",0);
            }
            objectList.add(rebJSONObject);
            retuJSONObject.put("list",objectList);
            return ResultUtil.success(retuJSONObject);
        }

    }

    /**
     * 防疫人员列表（QuarantinePerson/onShow）
     * */
    @PostMapping(WebApiServiceUrl.URL_QuarantinePersonOnShow)
    public Result QuarantinePersonOnShow(@Validated  @RequestBody OnShowVo onShowVo){
        if (onShowVo.getComId()==null){
            log.error("单位编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"单位编号为空");
        }

        if (onShowVo.getPage() == null){
            log.error("页码page为空");
            return ResultUtil.error(ResultEnum.Err_203,"页码page为空");
        }
        if (onShowVo.getSize() == null){
            log.error("页数size为空");
            return ResultUtil.error(ResultEnum.Err_203,"页数size为空");
        }
        return  quarantinePersonService.QuarantinePersonOnShow(onShowVo);
    }


    /**
     * 防疫汇总指标列表（QuarantinePerson/onYkmSum）
     * */
    @PostMapping(WebApiServiceUrl.URL_QuarantinePersonYkmSum)
    public Result QuarantinePersonYkmSum(@Validated  @RequestBody YkmSumVo ykmSumVo){
        return quarantinePersonService.QuarantinePersonYkmSum(ykmSumVo);
    }

    /**
     * 防疫人员核酸变化记录表（QuarantinePerson/hsResultList）
     * */
    @PostMapping(WebApiServiceUrl.URL_QuarantinePersonHsResultList)
    public Result QuarantinePersonHsResultList(@Validated  @RequestBody YkmSumVo ykmSumVo){
        return quarantinePersonService.QuarantinePersonHsResultList(ykmSumVo);
    }

    /**
     *  防疫门禁列表（QuarantineRecord/onShow）
     * */
    @PostMapping(AccessServiceUrl.URL_QuarantineRecordOnShow)
    public Result QuarantineRecordOnShow(@RequestBody AccessRecordVo accessRecordVo){
        log.info("防疫门禁列表请求参数：{{}}",accessRecordVo);
        return quarantinePersonService.QuarantineRecordOnShow(accessRecordVo);
    }

    /**
     * 区域统计列表（QuarantineAreaRecord/onShow）
     * */
    @PostMapping(AccessServiceUrl.URL_QuarantineAreaRecordOnShow)
    public Result QuarantineAreaRecordOnShow(@RequestBody @Validated YmDataGroupByMachVo ymDataGroupByMachVo){
        log.info("防疫门禁列表请求参数：{{}}",ymDataGroupByMachVo);
        return quarantinePersonService.QuarantineAreaRecordOnShow(ymDataGroupByMachVo);
    }
}
